<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/6/8
 * Time: 下午5:36
 */
namespace App\Api\Controllers\EasyPay;

use App\Models\EasypayConfig;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class BaseController
{
    public $host_url = 'https://180.168.215.67:4443/AG_MerchantManagementSystem_Core/agent/api/gen'; //根地址，测试地址
//    public $host_url = ''; //生产线

    public $upload_image = 'https://180.168.215.67:4443/AG_MerchantManagementSystem_Core/agent/api/imgUpl'; //图片上传，测试地址
//    public $upload_image = ''; //图片上传，生产线
    public $url;
    public $payment;
    public $signKey;

    function __construct()
    {
//        $this->url = 'https://notify-test.eycard.cn:7443/WorthTech_Access_AppPaySystemV2/apppayacc'; //测试线
//        $this->url = 'https://open.eycard.cn:8443/WorthTech_Access_AppPaySystemV2/apppayacc'; //生产线
        $this->url="https://phoenix.eycard.cn/aclTwo/WorthTech_Access_AppPaySystemV2/apppayacc";

        $this->payment = new Payment($this->url);

        $sign_key = Cache::get('easypay_key');
        if (!$sign_key) {
            $easypay_config_obj = EasypayConfig::where('config_id', '1234')->first();
            if (!$easypay_config_obj) {
                return [
                    'status' => '2',
                    'message' => '易生支付参数未配置'
                ];
            }

            if ($easypay_config_obj->getSignKey) {
                $sign_key = $easypay_config_obj->getSignKey;
            } else {
                $params = [
                    'channelid' => $easypay_config_obj->channel_id, //渠道编号
                    'opt' => 'getSign', //操作类型
                ];

                $key = $easypay_config_obj->sign; //渠道密钥

                $result = $this->payment->request($params, $key);
                Log::info('易生支付-更新并获取签名密钥');
                Log::info($result);
                //{"opt":"getSign","sign":"C068C8DC6573B676CA8C8819DB14E532","resultcode":"00","key":"5drliv52s0aidldifj8djkkk8528fjk8","channelid":"D01000000089888"}
                if(!$result) {
                    return [
                        'status' => '0',
                        'message' => '易生支付，未知错误'
                    ];
                }

                $result_arr = json_decode($result, true);
                if ($result_arr['resultcode'] == '00') {
                    $sign_key = $result_arr['key'];
                    $easypay_config_obj->update([
                        'getSignKey' => $sign_key
                    ]);
                    $res = $easypay_config_obj->save();
                    if (!$res) {
                        Log::info('易生支付-更新签名密钥-写表失败');
                    }
                } else {
                    return [
                        'status' => '0',
                        'message' => $result_arr['returnmsg']
                    ];
                }

                Cache::put('easypay_key', $sign_key, 525600);
            }
        }

        $this->signKey = $sign_key;
    }

}
